﻿@page "/Events/AddPointOnClick"
@using LiveChartsCore.Defaults
@using LiveChartsCore.Drawing
@using LiveChartsCore.SkiaSharpView.Blazor
@using ViewModelsSamples.Events.AddPointOnClick

<CartesianChart
	@ref="_chart"
	Series="ViewModel.SeriesCollection"
	OnPointerDownCallback="onPointerDown"
	TooltipPosition="LiveChartsCore.Measure.TooltipPosition.Hidden">
</CartesianChart>

@code {
	private CartesianChart _chart;

	public ViewModel ViewModel { get; set; } = new();

	private void onPointerDown(PointerEventArgs e)
	{
        // scales the UI coordintaes to the corresponging data in the chart.
        // ScaleUIPoint retuns an array of double
        var scaledPoint = _chart.ScaleUIPoint(new LvcPoint((float)e.OffsetX, (float)e.OffsetY));

        // where the X coordinate is in the first position
        var x = scaledPoint[0];

        // and the Y coordinate in the second position
        var y = scaledPoint[1];

        // finally add the new point to the data in our chart.
        ViewModel.Data.Add(new ObservablePoint(x, y));
	}
}
